﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Keyki.NefClass.Kernel.Fuzzy.FuzzySet
{
	public sealed class TriangleFuzzySet : AbstractFuzzySet
	{
		private double left;
		private double right;

		public TriangleFuzzySet()
			: this(Double.NaN, Double.NaN)
		{ }

		public TriangleFuzzySet(double left, double right)
		{
			this.right = right;
			this.left = left;
		}//TriangleFuzzySet

		public override void Init(IEnumerable<double> collection)
		{
			left = collection.Min();
			right = collection.Max();
		}//Init

		public override double Compute(double value)
		{
			double rezult = 0;
			double halfSize = (right - left) / 2;
			if (value > left && value < left + halfSize)
				rezult = (value - left) / halfSize;
			else
			{
				if (value > left + halfSize && value < right)
					rezult = (right - value) / halfSize;
			} //else

			return rezult;
		}//Compute

		public override AbstractFuzzySet Clone()
		{
			return new TriangleFuzzySet(this.left, this.right);
		}

	}//TriangleFuzzySet
}